package cn.zhentao.controller;

import cn.zhentao.borrow.dto.BorrowDto;
import cn.zhentao.borrow.dto.BorrowShow;
import cn.zhentao.borrow.dto.Sys_BrowListDto;
import cn.zhentao.borrow.param.BroCountParam;
import cn.zhentao.borrow.pojo.TBorrowing;
import cn.zhentao.common.Result;
import cn.zhentao.service.TBorrowingService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("borrow")
public class TBorrowingContro {
   @Autowired
    private TBorrowingService borrowingService;

   //借阅图书
   @PostMapping("borrow_book")
    public Result borrow_book(@RequestBody BorrowDto dto){
       Result bro = borrowingService.borrow_book(dto);
       return bro;
   }

   //图书查询
  @PostMapping("listBystatus")
  public Result listBystatus(@RequestBody BorrowShow show){
       return borrowingService.selusrbrolist(show);
  }

  //归还图书
  @PostMapping("book_back")
  public Result book_back(@RequestBody BorrowDto dto){
       return borrowingService.book_back(dto);
  }

  //统计图分析方法
  @GetMapping("/bro_data_Fx")
  public Result bro_data_Fx(@RequestParam("id") Integer id){
       if(id == 5){
           id=null;
       }
      List<BroCountParam> list = borrowingService.brocountlist(id);
      return Result.OK(list);
  }
 //用户预约
 @PostMapping("reser_path")
 public Result reser_path(@RequestBody BorrowDto dto){
     return borrowingService.yuyue(dto);
 }
 //用户取消预约
 @PostMapping("UnReser_path")
 public Result UnReser_path(@RequestBody BorrowDto dto){
     return borrowingService.UnYuyue(dto);
 }
//自动提醒预约的用户
 @GetMapping("Reser_title")
 public Result Reser_title(@RequestParam("uid") Integer uid){
       return borrowingService.Reser_title(uid);
 }
 //管理员端查询
  @PostMapping("borrowlist")
  public Result borrowlist(@RequestBody Sys_BrowListDto dto){
      Page<TBorrowing> list =borrowingService.Sys_list(dto);
       return Result.OK(list);
  }
  //删除借阅表信息
  @GetMapping("bro_del")
  public Result bro_del(@RequestParam("id") Integer id){
      TBorrowing byId = borrowingService.getById(id);
      //只有归还可以删除  其他不行
      if(byId.getStatus() == 0){
          return Result.ERROR("该书已被借阅，不可删除哦");
      }
       //这是在预约中
      if(byId.getStatus() == 3){
       return Result.ERROR("该书已被预约，不可删除哦");
      }
      if(byId.getStatus() == 2){
          return Result.ERROR("该书已被续约，不可删除哦");
      }
      borrowingService.removeById(id);
      return Result.OK("删除成功");
  }
}
